package com.css.ztb.exclude;

import com.css.ztb.interceptors.DataSourceInterceptor;
import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @title: MybatisConfig
 * @projectName gxb-clcpzr
 * @description: mybatis配置
 * @author: 赵鹏飞
 * @date 2020年05月19日 20:27
 */
@Configuration
@ConditionalOnBean(SqlSessionFactory.class)
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MybatisConfig {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @Autowired
    private DataSourceInterceptor dataSourceInterceptor;

    @PostConstruct
    public void addDynamicInterceptor() {
        if (CollectionUtils.isNotEmpty(sqlSessionFactoryList)) {
            for (final SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
                sqlSessionFactory.getConfiguration().addInterceptor(dataSourceInterceptor);
            }
        }
    }
}
